package cn.myfirefly.moming.portal.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.myfirefly.moming.common.model.MenuDto;
import cn.myfirefly.moming.common.model.RoleDataAuthDto;
import cn.myfirefly.moming.common.model.RoleDto;
import cn.myfirefly.moming.common.service.DataAuthObjectService;
import cn.myfirefly.moming.common.service.DataAuthTypeService;
import cn.myfirefly.moming.portal.model.FunctionDto;
import cn.myfirefly.moming.portal.model.RoleActionDto;
import cn.myfirefly.moming.portal.model.RoleFilterDto;
import cn.myfirefly.moming.portal.service.FunctionService;
import cn.myfirefly.moming.portal.service.RoleService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.StrUtil;

@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private FunctionService functionService;
	
	@Autowired
	private DataAuthTypeService dataAuthTypeService;
	
	@Autowired
	private DataAuthObjectService dataAuthObjectService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "role/index";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "role/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "role/edit";
	}
	
	@RequestMapping("setPermission.do")
	public String setPermission(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("roleSn", request.getParameter("roleSn"));
		request.setAttribute("funId", request.getParameter("funId"));
		List<MenuDto> list = functionService.getAllMenus();
		if (list != null) {
			request.setAttribute("menus", JSON.toJSONString(list));
		}
		return "role/set-permission";
	}
	
	@RequestMapping("getFunAction.do")
	public String getFunAction(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("roleSn", request.getParameter("roleSn"));
		// 根据功能id和roleSn，取得功能信息和动作信息
		request.setAttribute("funPermission", 
				roleService.getFunctionPermission(
						request.getParameter("funId"), 
						Integer.valueOf(request.getParameter("roleSn"))));
		
		// 取得数据权限信息
		RoleDataAuthDto roleDataAuthDto = roleService.getDataPermission(request.getParameter("funId"),
				Integer.valueOf(request.getParameter("roleSn")));
		request.setAttribute("dataPermission", roleDataAuthDto);
		request.setAttribute("dataAuthObjects", dataAuthObjectService.getList());
		request.setAttribute("dataAuthTypes", dataAuthTypeService.getList());
		return "role/fun-permission";
	}
	
	@RequestMapping("save.do")
	@ResponseBody
	public ResultDto save(RoleDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = roleService.save(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto list(RoleFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto result = roleService.getList(dto);
		return result;
	}
	
	@RequestMapping("getRole.do")
	@ResponseBody
	public ResultDto getRole(HttpServletRequest request, 
			HttpServletResponse response) {
		String roleSn = request.getParameter("roleSn");
		RoleDto role = roleService.getRole(Integer.valueOf(roleSn));
		List<RoleDto> list = new ArrayList<RoleDto>();
		list.add(role);
		return success(list);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(RoleDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = roleService.del(dto.getRoleSn());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
	@RequestMapping("savePermission.do")
	@ResponseBody
	public ResultDto savePermission(HttpServletRequest request, 
			HttpServletResponse response) {
		List<RoleActionDto> list = new ArrayList<RoleActionDto>();
		Integer roleSn = Integer.valueOf(request.getParameter("roleSn"));
		Integer funSn = Integer.valueOf(request.getParameter("funSn"));
		String dataAuthTypeStr = request.getParameter("dataAuthType");
		Integer dataAuthType = null;
		if (StrUtil.notBlank(dataAuthTypeStr)) {
			dataAuthType = new Integer(dataAuthTypeStr);
		}
		String dataAuthObjectStr = request.getParameter("dataAuthObject");
		Integer dataAuthObject = null;
		if (StrUtil.notBlank(dataAuthObjectStr)) {
			dataAuthObject = new Integer(dataAuthObjectStr);
		}
		String[] funRead = request.getParameterValues("funRead");
		handlePermission(funRead, roleSn, funSn, list);
		String[] funWrite = request.getParameterValues("funWrite");
		handlePermission(funWrite, roleSn, funSn, list);
		String funPermission = request.getParameter("funPermission");
		
		String msg = roleService.savePermission(roleSn, funSn, funPermission, list);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		
		// 保存数据权限
		msg = roleService.saveDataPermission(roleSn, funSn, dataAuthType, dataAuthObject);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		
		// 设置系统模块权限，更新角色系统表mm_role_sys
		FunctionDto functionDto = functionService.getFunction(funSn);
		msg = roleService.saveRoleSys(roleSn, functionDto.getSystemId());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	private void handlePermission(String[] funActionSn, 
			Integer roleSn, Integer funSn, List<RoleActionDto> list) {
		if (funActionSn != null) {
			RoleActionDto dto = null;
			for (int i = 0; i < funActionSn.length; i++) {
				dto = new RoleActionDto();
				dto.setFunActionSn(Integer.valueOf(funActionSn[i]));
				dto.setRoleSn(roleSn);
				dto.setFunSn(funSn);
				list.add(dto);
			}
		}
	}
}
